package com.example.timmy.myapplication.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.timmy.myapplication.R;
import com.example.timmy.myapplication.model.MainModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements BaseQuickAdapter.OnItemClickListener {


    private RecyclerView mRecyclerView;
    private MainAdapter mMainRecycleViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mMainRecycleViewAdapter = new MainAdapter(R.layout.item_main);
        mMainRecycleViewAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mMainRecycleViewAdapter.isFirstOnly(false);
        mRecyclerView.setAdapter(mMainRecycleViewAdapter);

        mMainRecycleViewAdapter.setOnItemClickListener(this);
    }

    private void initData() {

        List<MainModel> mainModels = new ArrayList<>();

        // TODO: 添加页面在这里
        mainModels.add(new MainModel("SpeechActivity 科大讯飞测试", SpeechActivity.class));
        mainModels.add(new MainModel("ExoPlayerActivity ExoPlayer 播放器测试", ExoPlayerActivity.class));
        mainModels.add(new MainModel("帧动画 OOM 测试", FrameAnimationActivity.class));
        mainModels.add(new MainModel("RecycleView 单独更新 Item 测试", NotifyItemActivity.class));
        mainModels.add(new MainModel("自定义进度条测试", LineViewActivity.class));
        mainModels.add(new MainModel("AndroidChat 实例 Demo", AndroidChatActivity.class));
        mainModels.add(new MainModel("AndroidChat 实例 Demo1", AndroidChatActivity1.class));
        mainModels.add(new MainModel("渐变进度条", ProgressRingActivity.class));
        mainModels.add(new MainModel("自定义折线图 LineChart", LineChartActivity.class));

        mMainRecycleViewAdapter.setNewData(mainModels);

        mMainRecycleViewAdapter.notifyDataSetChanged();
    }


    // Adapter 的点击事件
    @Override
    public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        MainModel item = (MainModel) baseQuickAdapter.getItem(position);

        Intent intent = new Intent(this, item.getActivity());
        startActivity(intent);
    }

    private class MainAdapter extends BaseQuickAdapter<MainModel, BaseViewHolder> {
        public MainAdapter(@LayoutRes int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, MainModel mainModel) {
            viewHolder.setText(R.id.title, mainModel.getTitle());
        }
    }
}
